package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.service.SmsReplyService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.sms.pojo.dto.SmsBalanceDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsDealingDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsMobileListDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsReplyLogListDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsSearchListDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsSignDTO;
import com.xbongbong.pro.sms.pojo.vo.SmsBalanceVO;
import com.xbongbong.pro.sms.pojo.vo.SmsDealingVO;
import com.xbongbong.pro.sms.pojo.vo.SmsMobileListVO;
import com.xbongbong.pro.sms.pojo.vo.SmsReplyLogListVO;
import com.xbongbong.pro.sms.pojo.vo.SmsSearchListVO;
import com.xbongbong.pro.sms.pojo.vo.SmsSignVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description:
 * @Author: hongxiao
 * @date: 2019-01-17 09:45
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/sms/reply")
public class SmsReplyController {

    @Resource
    private SmsReplyService smsReplyService;

    /**
     * 表单列表
     *
     * @param smsSearchListDTO 入参
     * @param br
     * @return
     * @author youli.chen
     * @addTime: 2019-11-21 13:41
     * @modified by:
     * @updateTime: 2019-11-21 13:41
     * @version
     * @since
     */
    @RequestMapping(value = "/getSmsSearchList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSmsSearchList(@RequestBody @Valid SmsSearchListDTO smsSearchListDTO, BindingResult br) {
        XbbResponse<SmsSearchListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsSearchListVO smsSearchListVO = smsReplyService.getSmsSearchList(smsSearchListDTO);
                response = new XbbResponse<>(smsSearchListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 查询当前公司是不是短信初始用户(有没有申请过签名)
     *
     * @param smsSignDTO 入参
     * @param br
     * @return
     * @author youli.chen
     * @addTime: 2019-11-21 13:41
     * @modified by:
     * @updateTime: 2019-11-21 13:41
     * @version
     * @since
     */
    @RequestMapping(value = "/smsSign", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String smsSign(@RequestBody @Valid SmsSignDTO smsSignDTO, BindingResult br) {
        XbbResponse<SmsSignVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsSignVO smsSignVO = smsReplyService.smsSign(smsSignDTO);
                response = new XbbResponse<>(smsSignVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 查询当前公司短信账户余额
     *
     * @param smsBalanceDTO 入参
     * @param br
     * @return
     * @author youli.chen
     * @addTime: 2019-11-21 13:41
     * @modified by:
     * @updateTime: 2019-11-21 13:41
     * @version
     * @since
     */
    @RequestMapping(value = "/getSmsBalance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSmsBalance(@RequestBody @Valid SmsBalanceDTO smsBalanceDTO, BindingResult br) {
        XbbResponse<SmsBalanceVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsBalanceVO smsBalanceVO = smsReplyService.getSmsBalance(smsBalanceDTO);
                response = new XbbResponse<>(smsBalanceVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 对象对应的号码(短信号码)
     *
     * @param smsMobileListDTO 入参
     * @param br
     * @return
     * @author youli.chen
     * @addTime: 2019-11-21 13:41
     * @modified by:
     * @updateTime: 2019-11-21 13:41
     * @version
     * @since
     */
    @RequestMapping(value = "/smsMobileList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String smsMobileList(@RequestBody @Valid SmsMobileListDTO smsMobileListDTO, BindingResult br) {
        XbbResponse<SmsMobileListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsMobileListVO smsMobileListVO = smsReplyService.smsMobileList(smsMobileListDTO);
                response = new XbbResponse<>(smsMobileListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 根据号码查询发送短信以及回复短信(短信往来)
     *
     * @param smsDealingDTO 入参
     * @param br
     * @return
     * @author youli.chen
     * @addTime: 2019-11-21 13:41
     * @modified by:
     * @updateTime: 2019-11-21 13:41
     * @version
     * @since
     */
    @RequestMapping(value = "/smsDealingList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String smsDealingList(@RequestBody @Valid SmsDealingDTO smsDealingDTO, BindingResult br) {
        XbbResponse<SmsDealingVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsDealingVO smsDealingVO = smsReplyService.smsDealingList(smsDealingDTO);
                response = new XbbResponse<>(smsDealingVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取回复短信日志列表
     *
     * @param smsReplyLogListDTO 入参
     * @param br
     * @return
     * @author youli.chen
     * @addTime: 2019-11-21 13:41
     * @modified by:
     * @updateTime: 2019-11-21 13:41
     * @version
     * @since
     */
    @RequestMapping(value = "/logList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String logList(@RequestBody @Valid SmsReplyLogListDTO smsReplyLogListDTO, BindingResult br) {
        XbbResponse<SmsReplyLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsReplyLogListVO smsReplyLogListVO = smsReplyService.logList(smsReplyLogListDTO);
                response = new XbbResponse<>(smsReplyLogListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
